<style>
    .processBarBg {border:1px solid #999999; width:98%; margin:5px; height:25px;line-height:25px;padding:1px; background:#EEEEEE;}
    .processBar {background:#3366cc; width:0px; padding-bottom:1px;overflow:hidden;}
</style>
<div class="tableform">
    <div class="division" id="express_tools_main">
	<form method='post' action='index.php?app=customs&ctl=admin_orders&act=refresh&view=2' id="form-order" name="form-order" isCloseDialog>
        <table width="100%" cellspacing="0" cellpadding="0" border="0" align="center" >
        <tbody>
            <tr>
                <td align="left">
                    <div class="division" style="display:none;" id="information">
                        本次处理 <span id="iTotal" style="color:#083E96"></span> 个发货单，
                        <span id="iSucc" style="color:green"></span> 个成功，
                        <span id="iFail" style="color:red"></span> 个失败，<span id="title">请稍候……</span>
                    </div>

                    <div id="processBarBg" class="processBarBg">
                        <div id="processBar" class="processBar">
                            &nbsp;
                        </div>
                    </div>
                    <div id="addlogino"></div>
                </td>
            </tr>

            <tr>
                <td align="center">
                    <{button label="开始进行申报" type="button" class="btn-primary" id="sync"}>
                    <{button label="关闭窗口" type="button" class="btn-primary" id="closeWicket"}>
                </td>
            </tr>
        </tbody>
        </table>
	</form>
    </div>
</div>

<script>
var itemData = <{$postIds}>;
var channel_id = 1;
var request_uri = "<{$request_uri}>";

var doTotal = 0;
var doSucc = 0;
var doFail = 0;

function ajaxget(idx) {
    if (idx >= getTotal()) {
        $('processBar').setStyle('width', '100%');
        $('title').set('html', '处理完成。');
        $('sync').hide();
        $('closeWicket').show();
        $('closeWicket').addEvent('click', function() {
			dosubmit();
        });
    }
    else {
      var id = itemData[idx];
      var _data = {'id':id,'channel_id':channel_id};
      new Request({
             url : "index.php?app=customs&ctl=admin_orders&act=async_cancel_declare",
             method : "post",
             data: _data,
             onComplete:function(result){
                 if(!result) return;
                 ret = JSON.decode(result);
                 if (ret['rsp'] == 'succ') {
                     doSucc++;
                 }
                 else {
                     doFail++;
                 }
                 doTotal++;
                 displayProcessInfo();
                 $('processBar').setStyle('width', (doTotal*100/(getTotal())) + '%');
                 ajaxget(idx+1);
             }
      }).send();
    }
}

function doRun() {
    //禁用开始按钮
    doTotal = 0;
    doSucc = 0;
    doFail = 0;
    displayProcessInfo();
    $('information').style.display ='';
    $('processBarBg').style.display ='';
    $('sync').disabled = true;
    $('sync').set('html', '<span><span>数据处理中，请稍候！</span></span>');
    ajaxget(0);
}

function getTotal() {
    var total = <{$count}>;
    return total;
}

/* 显示信息进度 */
function displayProcessInfo() {
    $('iTotal').set('html', getTotal());
    $('iSucc').set('html', doSucc);
    $('iFail').set('html', doFail);
}

doRun();
$('closeWicket').hide();

function dosubmit(){
    $("form-order").fireEvent('submit',{stop:function(){}});
}
</script>